package TGS;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TGS {

  private SecretKey kastgs;

  // Bytearray f�r den gemeinsammen Schl�ssel zwischen AS und tgs
  private byte[] key = { (byte) 0xFA, (byte) 0x67, (byte) 0xC1, (byte) 0xFF, (byte) 0xC3, (byte) 0x23, (byte) 0xED, (byte) 0x0B, (byte) 0x12,
      (byte) 0x6F, (byte) 0xBB, (byte) 0xA5, (byte) 0x68, (byte) 0xD4, (byte) 0xFD, (byte) 0xF8 };

  private KeyGenerator keygen;
  private Map<String, Date> ticketTimestamp;

  public TGS() {
    super();
    this.kastgs = new SecretKeySpec(key, "AES");
    ticketTimestamp = new HashMap<String, Date>();
    keygen = generateKey();
    keygen.init(128);
  }

  SecretKey getKastgs() {
    return kastgs;
  }
  
   Map<String, Date> getTicketTimestamp() {
    return ticketTimestamp;
  }
  
  KeyGenerator getKeygen() {
    return keygen;
  }
  
  public byte[] getKey() {
    return key;
  }
  
  
  private KeyGenerator generateKey() {
    try {
      return KeyGenerator.getInstance("AES");
    } catch (NoSuchAlgorithmException e) {
      System.out.println("Unbekannter Algorithmus fuer Schluesselgenerierung.");
      return null;
    }

  }

  private void connect() {
    ServerSocket s = null;
    Socket socket = null;

    try {
      s = new ServerSocket(20000);

      while (true) {
        System.out.println("TGS Waiting for connection..");
        socket = s.accept();

        (new TGSThread(socket, this)).start();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

  public static void main(String[] args) {
    TGS tgs = new TGS();
    tgs.connect();
  }

}
